﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CorpStoreServer
{
    class StartWin32App
    {
        private Log log;

        /// <summary>
        /// Start a win32 process whose path+filename is defined by 'exeName' parameter,
        /// and any command line arguments to it are defined by 'args' parameter.  
        /// 
        /// exeName=<name of process to run>
        /// args=<command line arguments surrounded by quotes>
        /// </summary>
        /// <param name="parms"></param>
        public void Run(Log log, NameValueCollection parms)
        {
            try
            {
                this.log = log;
                string exeName = GenLib.GetParm(parms, "exeName");
                string args = GenLib.GetParm(parms, "args");
                if (System.IO.File.Exists(parms.Get("exeName")))
                {
                    // exe exists - attempt to run it
                    Process.Start(exeName, args);
                }
                else
                    logErr("Cannot start win32 process '{0}' since it does not exist.  Check the filename and try again.", exeName);
            }
            catch (Exception ex)
            {
                logErr("Unhandled exception in startWin32App: {0}", ex.Message);
            }
        } // main


        private void logInfo(string msg, params object[] parms)
        {
            if (log != null)
                log.LogInfo(msg, parms);
        }


        private void logErr(string msg, params object[] parms)
        {
            if (log != null)
                log.LogErr(msg, parms);
        }

    }
}
